import axios from 'axios'
import store from '@/store'
import { getToken } from '@/utils/auth'
import { Message } from 'element-ui'
import Qs from 'qs'

var instance = axios.create({
    baseURL: "http://127.0.0.1:9886",
    timeout: 30000,
});

localStorage.setItem('baseURL', 'http://127.0.0.1:9886');

//白名单页面，不拦截
const whitelistPaths = 'api/User/login;'

//请求拦截
instance.interceptors.request.use(
    config => {
        if (whitelistPaths.indexOf(config.url) < 0) {
            config.headers.common['Authorization'] = "Bearer " + getToken();
        }
        if (config.json) {
            //post请求：接口指定传入json格式，则需以下处理
            config.headers.post['Content-Type'] = "application/json;charset=UTF-8";
            config.data = JSON.stringify(config.data);
        }
        else {
            config.data = Qs.stringify(config.data);
        }
        return config;
    },
    error => {
        console.log(error);
    }
)

//响应拦截
instance.interceptors.response.use(
    response => {
        //默认响应格式{"statusCode":200,"msg":"创建成功","data":null}
        var result = response.data;
        if (result.statusCode == 200) {
            if (response.config.method == "post" && response.config.url.indexOf("/api/User/login") == -1) {
                Message({
                    message: result.msg,
                    type: 'success',
                });
            }
            return result;
        } else if (result.statusCode == 403) {
            let shixaocount = localStorage.getItem("shixaocount");
            if (shixaocount == undefined || shixaocount == null) {
                Message({
                    message: result.msg,
                    type: 'error',
                    duration: 3 * 1000
                });
                localStorage.setItem("shixaocount", "1");
                setTimeout(() => {
                    store.dispatch('LoginOut').then(() => {
                        location.reload();
                    });
                }, 2000)
            }
        }
        else {
            Message({
                message: result.msg,
                type: 'error',
            });
            return false;
        }
    },
    error => {
        let response = error.response;
        let status =  response.data.statusCode;
        let msg = showStatus(status, response.data.msg);
        if (status == 403) {
            let shixaocount = localStorage.getItem("shixaocount");
            if (shixaocount == undefined || shixaocount == null) {
                Message({
                    message: msg,
                    type: 'error',
                    duration: 3 * 1000
                });
                localStorage.setItem("shixaocount", "1");
                setTimeout(() => {
                    store.dispatch('LoginOut').then(() => {
                        location.reload();
                    });
                }, 2000)
            }
        }
        else {
            Message({
                message: msg,
                type: 'error',
            });
        }
        return false;
    }
)

const showStatus = (status, msg) => {
    let message = ''
    switch (status) {
        case 400:
            message = '请求错误(400)'
            break
        case 401:
            message = '未登录，或登录失效，请重新登录'
            break
        case 403:
            message = '拒绝访问(403)'
            break
        case 404:
            message = '无法找到请求的资源(404)'
            break
        case 405:
            message = '方法不被允许(405)'
            break
        case 408:
            message = '请求超时(408)'
            break
        case 500:
            message = '服务器错误(500)'
            break
        case 501:
            message = '服务未实现(501)'
            break
        case 502:
            message = '网络错误(502)'
            break
        case 503:
            message = '服务不可用(503)'
            break
        case 504:
            message = '网络超时(504)'
            break
        case 505:
            message = 'HTTP版本不受支持(505)'
            break
        default:
            message = `连接出错(${status})!`
    }
    if (msg) {
        message = msg;
    }
    return message;
}

export default instance
